/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RunableClass.useClass;
import RunableSt.connect;
import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author Anatomy
 */
public class NewsTbRunnable implements Runnable {
JTable JT[];
    public NewsTbRunnable(JTable JT[]) {
        this.JT=JT;
    }
    
    
   String[] Col1 = {"วันที่และเวลา","ชื่อบริษัท","เรื่อง"};
    int countrow=-1; 
    @Override
    public void run() {
        ////////////////////AddNews To Table/////////////////////
         DefaultTableModel model1=new DefaultTableModel(Col1,0);
  for(;;){ 

   
   
    
        try {
            DefaultTableModel model=new DefaultTableModel(Col1,0);
        


            connect dbcon=new connect();
         String    sqlsec="SELECT COUNT(*) FROM `stock_news`";
            dbcon.rs= dbcon.stmt.executeQuery(sqlsec);
            dbcon.rs.next();
            int Rows=dbcon.rs.getInt(1);
            dbcon.CloseDB();
             connect dbcon1=new connect();
           // System.out.println(Rows);
            if(countrow!=Rows){
            String    sqlsec1="SELECT * FROM `stock_news`  ORder By  ABS(DateTime) DESC LIMIT 50";
            dbcon1.rs= dbcon1.stmt.executeQuery(sqlsec1);
             while(dbcon1.rs.next()){
                 String x[]={dbcon1.rs.getString(2),dbcon1.rs.getString(3),dbcon1.rs.getString(4)}; 
       
                // System.err.println(dbcon.rs.getString(2)+dbcon.rs.getString(3)+dbcon.rs.getString(4));
                 model.addRow(x);
             }
            countrow=Rows;
            model1=model;
             JT[0].setModel(model);
             JT[1].setModel(model);
          dbcon1.CloseDB();
            }else{
                JT[0].setModel(model1);
                JT[1].setModel(model1);
            }
               
             
        } catch (SQLException ex) {
        }
       // if(count==0){
        for(int i=0;i<2;i++){
       JT[i].setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
       JT[i].getColumnModel().getColumn(0).setResizable(false);
        JT[i].getColumnModel().getColumn(0).setPreferredWidth(120);
       JT[i].getColumnModel().getColumn(1).setResizable(false);
        JT[i].getColumnModel().getColumn(1).setPreferredWidth(80);
       JT[i].getColumnModel().getColumn(2).setResizable(true);
       JT[i].getColumnModel().getColumn(2).setPreferredWidth(1800);   
        }
      //  count=1;
      //  }
            
        
         try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
            }
    }
   // int count=0;
}
}
    

